{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from os.path import join, split, exists,  isdir,dirname,basename\n",
    "\n",
    "import os \n",
    "import sys \n",
    "os.chdir(dirname(os.getcwd()))\n",
    "\n",
    "\n",
    "sys.path.append(join(os.getcwd(),'base_utils'))\n",
    "\n",
    "import shutil\n",
    "from tools.utils import * \n",
    "import pdb\n",
    "from matplotlib import legend\n",
    "from datasets.loader import *  \n",
    "from tools.utils import * \n",
    "from datasets.loader import *\n",
    "from datasets.split_with_pidx import *\n",
    "\n",
    "import online_tracking_results_pb2\n",
    "from glob import glob \n",
    "\n",
    "\n",
    "from PIL import Image\n",
    "import io\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from multiprocessing import Pool, cpu_count\n",
    "\n",
    "def multiprocess_run(func, deal_list, n_cpus=None):\n",
    "    if n_cpus == None:\n",
    "        N_CPUS = cpu_count()\n",
    "    else:\n",
    "        N_CPUS = int(n_cpus)\n",
    "    print('running command with ' + str(N_CPUS) + ' CPUs')\n",
    "    pool = Pool(N_CPUS)\n",
    "    # results = pool.map(func, deal_list)\n",
    "    results = []\n",
    "    with tqdm(total=len(deal_list)) as pbar:\n",
    "        for result in pool.imap(func, deal_list):\n",
    "            results.append(result)\n",
    "            pbar.update(1)\n",
    "    pool.close()\n",
    "    pool.join()\n",
    "    return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "\n",
    "def save_online_tracking_results_pb2_tracks(online_tracking_results_pb2_tracks,save_path):\n",
    "    with open(save_path, \"wb\") as f:\n",
    "        f.write(online_tracking_results_pb2_tracks.SerializeToString())\n",
    "\n",
    "\n",
    "\n",
    "def read_save_online_tracking_results_pb2_tracks(file_name):\n",
    "    tracking_res = online_tracking_results_pb2.Tracks()\n",
    "        \n",
    "    with open(file_name, 'rb') as f:\n",
    "        # channel = os.path.basename(file_name).split(\"-\")[1]\n",
    "        tracking_res.ParseFromString(f.read())\n",
    "    return tracking_res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# root = '/root/exp/data/qamall_data/full_data/qa-mall-pb-adjustment/CR_zhongshan_wxhpoc_20240401_online_llyue_pb_s/20240613-210942'\n",
    "root = '/root/exp/data/qamall_data/full_data/qa-mall-pb-adjustment/CR_zhongshan_wxhpoc_20240401_online_llyue_pb_s/20241113-125902/'\n",
    " \n",
    "\n",
    "\n",
    "def extract_staff_score(src_sv_pb_files):\n",
    "\n",
    "    confidences = []\n",
    "    for idx, src_sv_pb_file in enumerate(tqdm(src_sv_pb_files)):\n",
    "        tracks = read_save_online_tracking_results_pb2_tracks(src_sv_pb_file)\n",
    "        if len(tracks.tracks) !=0:\n",
    "            for x in tracks.tracks:\n",
    "                # print(x.track_id, x.confidence, x.staff_result.confidence)\n",
    "                # image = Image.open(io.BytesIO(x.box_patches))\n",
    "                # image.save(join(bps_dir, '%s.jpg'%(x.track_id)))    \n",
    "                confidences.append(x.staff_result.confidence)\n",
    "\n",
    "    return confidences\n",
    "\n",
    "\n",
    "def core_code(src_sv_pb_file):\n",
    "    confidences = []\n",
    "    tracks = read_save_online_tracking_results_pb2_tracks(src_sv_pb_file)\n",
    "    if len(tracks.tracks) !=0:\n",
    "        for x in tracks.tracks:\n",
    "            confidences.append(x.staff_result.confidence)\n",
    "\n",
    "    return confidences\n",
    "\n",
    "def extract_staff_score_mp(src_sv_pb_files):\n",
    "\n",
    "    \n",
    "    return multiprocess_run(core_code, src_sv_pb_files,n_cpus = 64)\n",
    "\n",
    "\n",
    "# src_sv_pb_files = glob( join(root,'pbs', '*.pb'))\n",
    "# confidences = extract_staff_score(src_sv_pb_files)\n",
    "\n",
    "\n",
    "our_src_sv_pb_files = glob( join(root,'sc_pbs_staff_score_updated', '*.pb'))\n",
    "# our_confidences = extract_staff_score(our_src_sv_pb_files)\n",
    "our_confidences = extract_staff_score_mp(our_src_sv_pb_files)\n",
    "\n",
    "\n",
    "final_our_confidences= []\n",
    "for x in our_confidences:\n",
    "    final_our_confidences += x\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def plot_histogram(data_list):\n",
    "    \n",
    "    bins = np.linspace(0, 1, 21)\n",
    "    hist, _ = np.histogram(data_list, bins=bins)\n",
    "\n",
    "    \n",
    "    bar_plot = plt.bar(bins[:-1], hist, width=(bins[1] - bins[0]), align='edge')\n",
    "    plt.xlabel('staff score')\n",
    "    plt.ylabel('count')\n",
    "    # plt.yscale('symlog')\n",
    "    plt.xticks(bins,rotation = 45, )\n",
    "    plt.title('distribution of staff score')\n",
    "\n",
    "\n",
    "    last_bar = bar_plot[-1]\n",
    "    height = last_bar.get_height()\n",
    "    plt.text(last_bar.get_x() + last_bar.get_width() / 2, height + 0.5, str(height), ha='center', va='bottom')\n",
    "    plt.grid(True)\n",
    "\n",
    "    plt.savefig('baseline_score_distribution.png')\n",
    "    plt.show()\n",
    "    \n",
    "\n",
    "# data = confidences\n",
    "plot_histogram(final_our_confidences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import glob \n",
    "from PIL import Image\n",
    "a = \"/root/exp/fastreid/datasets/mall_fashion/CR_zhongshan_wxhpoc5_test_for_eval/bounding_box_test\"\n",
    "imgs = sorted(glob.glob(a+'/*.jpg'))\n",
    "\n",
    "gallery_length = len(imgs)\n",
    "\n",
    "rimgs =  [Image.open(x) for x in imgs]\n",
    "\n",
    "merged = []\n",
    "for x in range(gallery_length//2):\n",
    "    merged.append(merge_height(rimgs[x*2:x*2+2]))\n",
    "final = merge_width(merged)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# generation labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "def extract_annotation(annotation,target_label ):\n",
    "    assert target_label is not None,'target_label should be given '\n",
    "\n",
    "    \n",
    "    ans = {}\n",
    "    for k,v in annotation.items():\n",
    "        pid = k.split('_')[0]\n",
    "        if pid.startswith('id'):\n",
    "            #* label 1 indicates staff  \n",
    "            if v['label_result']['personType'] in target_label:\n",
    "                # ans[pid] = [True if v['label_result']['personType']== 1 else False,\n",
    "                #                 v['image_url']]\n",
    "                # ans[pid] = False if v['label_result']['personType']==0 else True\n",
    "                ans[pid]  = v['image_url']\n",
    "    return ans \n",
    "\n",
    "\n",
    "\n",
    "gallery_img_path = '/root/exp/data/qamall_data/full_data/qa-mall-pb-adjustment/CR_zhongshan_wxhpoc_20240401_online_llyue_pb_s/20241113-125902/sc_body_patches'\n",
    "\n",
    "\n",
    "root = '/root/exp/fastreid/jupyters/cpu084_jhe'\n",
    "\n",
    "\n",
    "pid_label = load_json(join(root,\"labeling_result_20241029095720.json\"))\n",
    "\n",
    "\n",
    "#* only extract staff\n",
    "staff_label = extract_annotation(pid_label,[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "satff_pids = list(staff_label.keys())\n",
    "vtp = load_json(join(root,\"20240401_event_bak/trajectory_vtp_whole_day/realtime.vtp.json\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 4050027/4050027 [00:04<00:00, 848979.10it/s] \n"
     ]
    }
   ],
   "source": [
    "pid2tid = {}\n",
    "\n",
    "for tid,pid in tqdm(vtp.items()):\n",
    "\n",
    "    tmp = pid2tid.get(pid,[])\n",
    "\n",
    "    tmp.append(tid)\n",
    "    pid2tid[pid] = tmp "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "680\n"
     ]
    }
   ],
   "source": [
    "staff_pid2tids = {pid:tids  for pid,tids in pid2tid.items() if pid in satff_pids}\n",
    "\n",
    "print(len(staff_pid2tids),)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "680\n",
      "680\n",
      "15525\n"
     ]
    }
   ],
   "source": [
    "print(len(staff_pid2tids))\n",
    "staff_pid2tidlens = {pid: len(tids) for pid,tids in staff_pid2tids.items()}\n",
    "\n",
    "\n",
    "lengths = sorted(list(staff_pid2tidlens.values()),reverse= True) \n",
    "print(len(lengths))\n",
    "print((np.array(lengths) <= 500).sum() * 27)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def annotation_sample_geneation():\n",
    "\n",
    "    num_per_pid = 10\n",
    "    target_dir = '/root/exp/fastreid/logs/ImageTag_CR_zhongshan_wxhpoc_0401'\n",
    "    tmp_save_root = '/root/exp/fastreid/logs/zs_0401_label1'\n",
    "    for pid, tids in  tqdm(staff_pid2tids.items()):\n",
    "\n",
    "        length = len(tids)\n",
    "\n",
    "        if length >  500:\n",
    "            print('skip, because %s length is %d'%(pid,length))\n",
    "            continue\n",
    "\n",
    "        \n",
    "        tid_imgs = [Image.open(join(gallery_img_path,t + '.jpg')) for t in tids]\n",
    "        tid_img_sizes = [np.array(list(x.size)).prod() for x in tid_imgs]\n",
    "        max_indexes = np.argsort(np.array(tid_img_sizes))[::-1]\n",
    "\n",
    "        sorted_tid_imgs = np.array(tid_imgs)[max_indexes]\n",
    "\n",
    "        if len(sorted_tid_imgs) >8:\n",
    "            sorted_tid_imgs = sorted_tid_imgs[:num_per_pid]\n",
    "\n",
    "        final_pid_repre = merge_height(sorted_tid_imgs)\n",
    "        # final_pid_repre.save(join(tmp_save_root,pid+'.jpg'))\n",
    "\n",
    "        \n",
    "        for x in range(gallery_length//2):\n",
    "            cloth_pair = merge_height(rimgs[x*2:x*2+2])\n",
    "\n",
    "            final_annotation_pair = merge_width([cloth_pair, final_pid_repre])\n",
    "\n",
    "            final_annotation_pair.save(join(target_dir, pid +'_'+imgs[x*2].split('/')[-1].split('_')[0]+'.jpg'))\n",
    "\n",
    "        \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "annotation pid number is 8298\n",
      "labels: {0, 1, 2, 3, 4, 5}\n",
      "number of annotation pid is 8298 \t pids extracted by corresponding store name from filter.csv is 136\n",
      "union pid number is 0\n"
     ]
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fastreid",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
